import { AppConfig } from '../../environments/environment';
import * as url from 'url';
import * as path from 'path';

export class CommonUtils {
    static guid() {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
            const r = Math.random() * 16 | 0;
            const v = c === 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
    }
    /**
     * 探测buffer数据的字符集编码
     * @param data buffer数据
     */
    static detectBufferCharset(data: Buffer) {
        const jschardet = require('jschardet');
        return jschardet.detect(data);
    }
    /**
     * 解码buffer数据为字符串
     * @param data buffer数据
     */
    static decodeBufferToString(data: Buffer): string {
        let encoding = 'UTF-8';
        const charset = CommonUtils.detectBufferCharset(data);
        if (charset) {
            if (charset.encoding === 'GB2312') {
                encoding = 'GBK';
            } else if (charset.encoding === 'BIG5') {
                encoding = 'BIG5';
            } else if (charset.encoding === 'EUC-TW') {
                encoding = 'EUC-TW';
            } else if (charset.encoding === 'ISO-2022-CN') {
                encoding = 'ISO-2022-CN';
            } else if (charset.encoding === 'ASCII') {
                encoding = 'ASCII';
            } else if (charset.encoding === 'UTF-16') {
                encoding = 'UTF-16';
            } else if (charset.encoding === 'UTF-32') {
                encoding = 'UTF-32';
            }
        }
        return CommonUtils.decodeUseIconv(data, encoding);
    }
    /**
     * 采用iconv-lite库解码数据
     * @param data 待解码数据
     * @param encoding 编码类型
     */
    static decodeUseIconv(data: any, encoding: string) {
        const iconv = require('iconv-lite');
        iconv.skipDecodeWarning = true;
        return iconv.decode(data, encoding);
    }
    /**
     * 将base64字符串转换为uri形式数据
     * @param base64 base64字符串
     * @param type 类型，如：image/jpeg
     */
    static makeURI (base64, type) {
        return 'data:' + type + ';base64,' + base64;
    }
    /**检查是否为Http路径 */
    static isHttpPath(str: string) {
        if (!str) {
            return false;
        }
        const tmp = url.parse(str);
        if (tmp && (tmp.protocol === 'http:' || tmp.protocol === 'https:')) {
            return true;
        }
        return false;
    }
    /**
     * 获取资源中的uri
     * @param uri uri相对路径
     */
    static getResourceUri (uri: string) {
        let destUrl: string;
        destUrl = uri;
        if (!destUrl || CommonUtils.isHttpPath(uri)) {
            return destUrl;
        }
        if (!CommonUtils.isHttpPath(location.href)) {
            if (AppConfig.production) {
                destUrl =  url.format({
                    pathname: path.join(__dirname, `/resources/app.asar/dist/${uri}`),
                    protocol: 'file:',
                    slashes: true,
                });
            } else {
                destUrl =  url.format({
                    pathname: path.join(__dirname, uri),
                    protocol: 'file:',
                    slashes: true,
                });
            }
        }
        return destUrl;
    }
    /**获取是否为图片文件(jpg、jpeg、png、gif、bmp格式) */
    static isImage(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.jpg' || ext === '.jpeg'
        || ext === '.png' || ext === '.gif' || ext === '.bmp';
    }
    /**获取是否为jpg图片文件(jpg、jpeg) */
    static isJpgImage(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.jpg' || ext === '.jpeg';
    }
    /**获取是否为png图片文件 */
    static isPngImage(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.png';
    }
    /**获取是否为gif图片文件 */
    static isGifImage(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.gif';
    }
    /**获取是否为bmp图片文件 */
    static isBmpImage(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.bmp';
    }
    /**获取是否为mp4文件(mp4) */
    static isMp4(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.mp4';
    }
    /**获取是否为ppt文件(ppt、pptx格式) */
    static isPPT(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.ppt' || ext === '.pptx';
    }
    /**获取是否为word文件(doc、docx格式) */
    static isWord(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.doc' || ext === '.docx';
    }
    /**获取是否为zip文件(zip格式) */
    static isZip(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.zip';
    }
    /**获取是否为txt文件(txt格式) */
    static isTxt(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.txt';
    }
    /**获取是否为json文件(json格式) */
    static isJson(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.json';
    }
    /**获取是否为xml文件(xml格式) */
    static isXml(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.xml';
    }
    /**获取是否为ini文件(ini格式) */
    static isIni(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.ini';
    }
    /**获取是否为config文件(config、cfg格式) */
    static isConfig(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.config' || ext === '.cfg';
    }
    /**获取是否为html文件(html、htm格式) */
    static isHtml(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.html' || ext === '.htm';
    }
    /**获取是否为cs文件(.cs格式) */
    static isCS(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.cs';
    }
    /**获取是否为xaml文件(.xaml格式) */
    static isXaml(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.xaml';
    }
    /**获取是否为sql文件(.sql格式) */
    static isSql(ext: string) {
        ext = ext.toLowerCase();
        return ext === '.sql';
    }
    /**获取是否为文本格式(*.txt , *.json , *.xml , *.ini , *.config , *.cfg , *.html , *.htm , *.cs , *.xaml , *.sql)) */
    static isTextFormat(ext: string) {
        return this.isTxt(ext) || this.isJson(ext) || this.isXml(ext) || this.isIni(ext)
        || this.isConfig(ext) || this.isHtml(ext) || this.isCS(ext) || this.isXaml(ext)
        || this.isSql(ext);
    }
}
